/**
 * This module contains all the objects that are used only during the extension
 *  initialization and, symmetrically, on shutdown. The main entry point is the
 *  "StartupEvents.onAppStartup" method.
 */

var Ci = Components.interfaces;
var Cc = Components.classes;
var Cr = Components.results;
var Cu = Components.utils;

// Define symbols available to users of this JavaScript Module
var EXPORTED_SYMBOLS = ["StartupEvents"];

/**
 * This object handles extension startup and shutdown, and acts as bookkeeper
 *  for the related observer registrations. Actual work is delegated to the
 *  StartupInitializer object.
 */
var StartupEvents = {

  /**
   * This function must be called once on application startup, and simply
   *  registers with the host application for other notifications, to be
   *  handled by the "observe" function.
   */
  onAppStartup: function() {
    this._observerService.addObserver(this, "profile-after-change", false);
    this._observerService.addObserver(this, "xpcom-shutdown", false);
  },

  /** Called when it is time to unregister all the observers. */
  onAppShutdown: function() {
    this._observerService.removeObserver(this, "profile-after-change");
    this._observerService.removeObserver(this, "xpcom-shutdown");
  },


  afterProfileChange: function() {
      StartupWelcome.welcomeDialog();
  },

  // --- nsIObserver interface functions ---

  /**
   * This function handles all the notifications related to application startup
   *  and shutdown. The aTopic argument is used to distinguish between
   *  notifications.
   */
  observe: function(aSubject, aTopic, aData) {
    if (aTopic == "profile-after-change") { this.afterProfileChange(); }
    if (aTopic == "xpcom-shutdown")       { this.onAppShutdown();      }
  },

  // --- Private methods and properties ---

  _observerService: Cc["@mozilla.org/observer-service;1"]
   .getService(Ci.nsIObserverService)
};


/**
 * Provides a global method to display the welcome dialog. This method can be
 *  called even if no other window is visible.
 */
var StartupWelcome = {

  welcomeDialog: function() {
    // Opens the window using the window watcher component
    /*Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher).
    openWindow(null, "chrome://ballbet/content/loginDialog.xul",
               "login-welcomeDialog", "chrome,dialog,centerscreen,modal", null);
    */
    var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
    var username = {value: ""};              // default the username to user

    var password = {value: ""};              // default the password to pass

    var check = {value: false};                   // default the checkbox to true

    var title = "Login:";
    var ok = "";
    
    var yinquan = {name: "yinquan", password: "1102012"};
    var xxbb = {name: "xxbb", password: "123456"};
    return;
    var result = prompts.promptUsernameAndPassword(null, title, "",
                                               username, password, ok, check);
    if(result && (username.value== yinquan.name && password.value==yinquan.password ||
                  username.value==xxbb.name && password.value==xxbb.password) ) {
      
    } else {
      var appStartup = Cc['@mozilla.org/toolkit/app-startup;1'].getService(Ci.nsIAppStartup);
      appStartup.quit(Ci.nsIAppStartup.eForceQuit);
    }

  }   

};

